package cn.edu.lingnan.services;

import cn.edu.lingnan.dao.CourseMapper;
import cn.edu.lingnan.pojo.Course;
import cn.edu.lingnan.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

public class CourseServiceMysqlImpl implements CourseService {

    @Override
    public List<Course> findAllCourse() {
        List<Course> list = null;
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
        list = courseMapper.findAllCourse();
        return list;
    }

    @Override
    public Course findCourseById(String cid) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
        Course course = courseMapper.findCourseById(cid);
        return course;
    }

    //-------------------------------------------------------------------------------
    @Override
    public List<Course> findCourseByNameUseLike(String cname) {
        List<Course> courses = null;
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
        courses = courseMapper.findCourseByNameUseLike(cname);
        sqlSession.close();
        return courses;
    }

    @Override
    public int insertCourse(Course course) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
        int i = courseMapper.insertCourse(course);
        sqlSession.commit();
        return i;
    }

    @Override
    public int updateCourse(Course course) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
        int i = courseMapper.updateCourse(course);
        sqlSession.commit();
        return i;
    }

    @Override
    public int updateCoursePassword(Map map) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
        int i = courseMapper.updateCoursePassword(map);
        sqlSession.commit();
        return i;
    }

    @Override
    public int deleteCourse(String cid) {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
        int i = courseMapper.deleteCourse(cid);
        sqlSession.commit();
        return i;
    }

    @Override
    public List<Course> findCourseUseIf(Map map) {
        List<Course> list = null;
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
        list = courseMapper.findCourseUseIf(map);
        return list;
    }
}
